package com.saasitems.admin.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.saasitems.admin.web.vo.ResultVO;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import javax.servlet.http.HttpServletRequest;

/**
 * @description：基础 controller
 * @author：zhixuan.wang
 * @date：2015/10/1 14:51
 */
public abstract class BaseController {
    // 控制器本来就是单例，这样似乎更加合理
    protected Logger logger = LogManager.getLogger(getClass());

//    @Autowired
//    private IUserService userService;

    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        /**
         * 自动转换日期类型的字段格式
         */
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));

        /**
         * 日期
         */
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
        /**
         * 防止XSS攻击
         */
        // binder.registerCustomEditor(String.class, new StringEscapeEditor());
    }

//    /**
//     * 获取当前登录用户对象
//     * @return
//     */
//    public User getCurrentUser() {
//        ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
//        User currentUser = userService.selectById(user.getId());
//        return currentUser;
//    }

//    /**
//     * 获取当前登录用户id
//     * @return
//     */
//    public Long getUserId() {
//        return this.getCurrentUser().getId();
//    }

//    /**
//     * 获取当前登录用户名
//     * @return
//     */
//    public String getStaffName() {
//        return this.getCurrentUser().getName();
//    }

    /**
     * ajax失败
     * @param msg 失败的消息
     * @return {Object}
     */
    public Object renderError(String msg) {
        ResultVO result = new ResultVO();
        result.setInfo(msg);
        return result;
    }

    /**
     * ajax成功
     * @return {Object}
     */
    public Object renderSuccess() {
        ResultVO result = new ResultVO();
        result.setStatus(ResultVO.SUCCESS);
        return result;
    }

    /**
     * ajax成功
     * @param msg 消息
     * @return {Object}
     */
    public Object renderSuccess(String msg) {
        ResultVO result = new ResultVO();
        result.setStatus(ResultVO.SUCCESS);
        result.setInfo(msg);
        return result;
    }

    /**
     * ajax成功
     * @param msg
     * @param url
     * @return
     */
    public Object renderSuccess(String msg, String url) {
        ResultVO result = new ResultVO();
        result.setStatus(ResultVO.SUCCESS);
        result.setInfo(msg);
        result.setUrl(url);
        return result;
    }

    /**
     * 生成完整url，先按照此使用
     * @return
     */
    public String convertUrl(HttpServletRequest request, String url) {
        String ctx = request.getContextPath();
        String ret = "";
        if (StringUtils.isBlank(url)) {
            ret = ctx;
        } else {
            if (url.startsWith("/")) {
                ret = ctx + url;
            } else {
                ret = ctx + "/" + url;
            }
        }
        return ret;
    }
}
